final$tert_pvi <- as.numeric(cut(final$pvi_state, quantile(final$pvi_state, c(0, 1/3, 2/3, 1)), include.lowest = TRUE))

violent1_pvi <- lm(violence_range ~ days * treatment, final %>% filter(tert_pvi==1, days <= 150), weights = weight)

violent2_pvi <- lm(violence_range ~ days * treatment, final %>% filter(tert_pvi==2, days <= 150), weights = weight)

violent3_pvi <- lm(violence_range ~ days * treatment, final %>% filter(tert_pvi==3, days <= 150), weights = weight)

violent1_pvi <- 
  data.frame(get_model_data(violent1_pvi, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

violent2_pvi <- 
  data.frame(get_model_data(violent2_pvi, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

violent3_pvi <- 
  data.frame(get_model_data(violent3_pvi, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

kk1 <- violent1_pvi %>% filter(x == 0, group < 1) %>% mutate(which = '1')
kk2 <- violent1_pvi %>% filter(x == 1, group > -1) %>% mutate(which = '1')
kk3 <- violent2_pvi %>% filter(x == 0, group < 1) %>% mutate(which = '2')
kk4 <- violent2_pvi %>% filter(x == 1, group > -1) %>% mutate(which = '2')
kk5 <- violent3_pvi %>% filter(x == 0, group < 1) %>% mutate(which = '3')
kk6 <- violent3_pvi %>% filter(x == 1, group > -1) %>% mutate(which = '3')

n1 <- 
  ggplot()+
  geom_line(data = kk1, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = kk1, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = kk2, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = kk2, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = kk3, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = kk3, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = kk4, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = kk4, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = kk5, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = kk5, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = kk6, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = kk6, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_hline(yintercept = 0, color = 'red', linetype=2)+
  xlim(-.13,.5)+
  scale_y_continuous(breaks = seq(-50,150,10))+
  ylab('Days before/after 2022 Election')+
  xlab('Support for Political Violence')+
  coord_flip()+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9"),
                     labels=c('Low PVI', 'Medium PVI', 'High PVI'))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9"),
                    labels=c('Low PVI', 'Medium PVI', 'High PVI'))+
  scale_linetype(labels=c('Low PVI', 'Medium PVI', 'High PVI'))+
  labs(fill = "PVI", linetype = 'PVI', color = 'PVI')+
  theme_bw()


norms1_pvi <- lm(norms_range ~ days * treatment, final %>% filter(tert_pvi==1, days <= 150), weights = weight)

norms2_pvi <- lm(norms_range ~ days * treatment, final %>% filter(tert_pvi==2, days <= 150), weights = weight)

norms3_pvi <- lm(norms_range ~ days * treatment, final %>% filter(tert_pvi==3, days <= 150), weights = weight)

norms1_pvi <- 
  data.frame(get_model_data(norms1_pvi, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

norms2_pvi <- 
  data.frame(get_model_data(norms2_pvi, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

norms3_pvi <- 
  data.frame(get_model_data(norms3_pvi, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

uu1 <- norms1_pvi %>% filter(x == 0, group < 1) %>% mutate(which = '1')
uu2 <- norms1_pvi %>% filter(x == 1, group > -1) %>% mutate(which = '1')
uu3 <- norms2_pvi %>% filter(x == 0, group < 1) %>% mutate(which = '2')
uu4 <- norms2_pvi %>% filter(x == 1, group > -1) %>% mutate(which = '2')
uu5 <- norms3_pvi %>% filter(x == 0, group < 1) %>% mutate(which = '3')
uu6 <- norms3_pvi %>% filter(x == 1, group > -1) %>% mutate(which = '3')

n2 <- 
  ggplot()+
  geom_line(data = uu1, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = uu1, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = uu2, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = uu2, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = uu3, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = uu3, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = uu4, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = uu4, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = uu5, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = uu5, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = uu6, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = uu6, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_hline(yintercept = 0, color = 'red', linetype=2)+
  xlim(0,.7)+
  scale_y_continuous(breaks = seq(-50,150,10))+
  ylab('Days before/after 2022 Election')+
  xlab('Support for Norm Violations')+
  coord_flip()+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9"),
                     labels=c('Low PVI', 'Medium PVI', 'High PVI'))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9"),
                    labels=c('Low PVI', 'Medium PVI', 'High PVI'))+
  scale_linetype(labels=c('Low PVI', 'Medium PVI', 'High PVI'))+
  labs(fill = "PVI", linetype = 'PVI', color = 'PVI')+
  theme_bw()


affpol1_pvi <- lm(affpol ~ days * treatment, final %>% filter(tert_pvi==1), weights = weight)

affpol2_pvi <- lm(affpol ~ days * treatment, final %>% filter(tert_pvi==2), weights = weight)

affpol3_pvi <- lm(affpol ~ days * treatment, final %>% filter(tert_pvi==3), weights = weight)

affpol1_pvi <- 
  data.frame(get_model_data(affpol1_pvi, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

affpol2_pvi <- 
  data.frame(get_model_data(affpol2_pvi, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

affpol3_pvi <- 
  data.frame(get_model_data(affpol3_pvi, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

rr1 <- affpol1_pvi %>% filter(x == 0, group < 1) %>% mutate(which = '1')
rr2 <- affpol1_pvi %>% filter(x == 1, group > -1) %>% mutate(which = '1')
rr3 <- affpol2_pvi %>% filter(x == 0, group < 1) %>% mutate(which = '2')
rr4 <- affpol2_pvi %>% filter(x == 1, group > -1) %>% mutate(which = '2')
rr5 <- affpol3_pvi %>% filter(x == 0, group < 1) %>% mutate(which = '3')
rr6 <- affpol3_pvi %>% filter(x == 1, group > -1) %>% mutate(which = '3')

n3 <- 
  ggplot()+
  geom_line(data = rr1, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = rr1, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = rr2, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = rr2, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = rr3, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = rr3, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = rr4, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = rr4, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = rr5, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = rr5, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = rr6, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = rr6, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_hline(yintercept = 0, color = 'red', linetype=2)+
  xlim(0,100)+
  scale_y_continuous(breaks = seq(-50,150,10))+
  ylab('Days before/after 2022 Election')+
  xlab('Affective Polarization')+
  coord_flip()+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9"),
                     labels=c('Low PVI', 'Medium PVI', 'High PVI'))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9"),
                    labels=c('Low PVI', 'Medium PVI', 'High PVI'))+
  scale_linetype(labels=c('Low PVI', 'Medium PVI', 'High PVI'))+
  labs(fill = "PVI", linetype = 'PVI', color = 'PVI')+
  theme_bw()

nn2 <- suppressMessages(ggpubr::ggarrange(n1,n2,n3, ncol = 1))
suppressMessages(print(nn2))
#ggsave(nn2, file = 'pvi_3panel.png', units = 'in', height = 8, width = 10)